/**
 * @program: PavtFirmware
 * @description:
 * @author: zyc
 * @create: 2018-07-02 09:27
 **/
package com.zjpavt.client.hal;

import com.zjpavt.client.Connection;
import com.zjpavt.client.hal.network.NetworkManager;
import com.zjpavt.client.util.*;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author zyc
 * 默认网关切换处理类
 * 通过调用脚本的方式切换默认路由，使得发送出去的数据是通过 以太网 或者 4G
 */
@Slf4j
public class RouteDefaultController {
    private static final String[] ROUTE_ARRAYS = {"eth0","eht1","ppp0"};
    Route route = Route.ETH0;
    private static final RouteDefaultController INSTANCE = new RouteDefaultController();
    private RouteDefaultController() {
    }

    public static RouteDefaultController getINSTANCE() {
        return INSTANCE;
    }

    public enum Route {
        /**
         * eth0 通常为以太网口1
         */
        ETH0("eth0", "sh " + ClientConfig._4G_SHELL_PATH + " eth0", "NETWORK_ENTE"),
        /**
         * eth1通常为以太网口2
         */
        ETH1("eht1","sh " + ClientConfig._4G_SHELL_PATH + " eth1",""),
        /**
         * ppp0 为4G网络通讯口
         */
        PPP0("ppp0","sh " + ClientConfig._4G_SHELL_PATH + " 4g","4G");
        /**
         * route名称
         * 使用ifconfig可以看到
         */
        String route;
        /**
         * shell脚本内容
         */
        String shellContent;
        /**
         * 网络状态名称，对应显示屏幕上的显示字符
         */
        String networkStatus;
        Route(String route, String shellContent,String networkStatus) {
            this.shellContent = shellContent;
            this.route = route;
            this.networkStatus = networkStatus;
        }

        public String getRoute() {
            return route;
        }

        public String getShellContent() {
            return shellContent;
        }

    }

    /**
     * 通过Route类型，将默认路由切换制指定路由
     * @param route
     */
    public void changeDefaultRoute(Route route) {
        /*        String oldRoute = NetworkManager.getInstance().getActiveNetworkName();
                String newRoute2Change = route.getRoute();
                Process process = null;
                BufferedReader bufferedReader = null;
                String outputInfomation;*/
        try {
            /*process = Runtime.getRuntime().exec(route.getShellContent());
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((outputInfomation = bufferedReader.readLine()) != null) {
                log.debug(outputInfomation);
            }
            this.route = route;
            process.waitFor();*/
            ProcessUtil.runCommandWait(route.getShellContent(),5000);
            NetworkManager.getInstance().updateCurrentNetworkByRoute();
//            if (StringUtil.equals(oldRoute,NetworkManager.getInstance().getActiveNetworkName()) && newRoute2Change!= null && !newRoute2Change.equals(NetworkManager.getInstance().getActiveNetworkName())) {
//                Connection.getInstance().close();
//            }
        } catch (IOException e) {
            log.error(LoggerUtil.convertException2String(e));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
